<template>
	<view class="my-settle-container">
		<label class="radio" @click="changeAllState">
			<radio color="#C00000" :checked="isFullCheck" /><text>全选</text>
		</label>
		<view class="amount">
			合计：<text>￥{{checkedTotalPrice}}</text>
		</view>
		<view class="btn-settle" @click="settlement">结算({{checkedCount}})</view>
	</view>
</template>

<script>
	import {mapGetters, mapMutations, mapState} from 'vuex';
	
	export default {
		name:"my-settle",
		data() {
			return {
				seconds: 3, //倒计时秒数
				timer: null, //定时器 id
			};
		},
		computed: {
			//选中商品的数量，商品总数，选中商品的总价
			...mapGetters('m_cart', ['checkedCount', 'total', 'checkedTotalPrice']),
			//详细地址
			...mapGetters('m_user', ['addrDetail']),
			//登录后的 token
			...mapState('m_user', ['token']),
			//购物车中的商品
			...mapState('m_cart', ['cart']),
			//判断是否全选
			isFullCheck(){
				return this.total === this.checkedCount;
			},
		},
		methods: {
			...mapMutations('m_cart', ['updateAllGoodsState']),
			...mapMutations('m_user', ['updateRedirectInfo']),
			//点击全选
			changeAllState(){
				this.updateAllGoodsState(!this.isFullCheck);
			},
			//点击结算按钮
			settlement(){
				if(!this.checkedCount){
					//未选中任何商品
					return uni.$showMsg('请先选择商品！');
				}
				
				if(!this.addrDetail){
					//未选择收货地址
					return uni.$showMsg('请先选择收货地址！');
				}
				
				if(!this.token){
					//未登录
					// return uni.$showMsg('请先登录！');
					return this.delayNavigate();
				}
				
				this.payOrder();
			},
			//显示倒计时提示
			showTips(n){
				uni.showToast({
					title: '您尚未登录，' + n + '秒后跳转到登录页',
					icon: 'none',
					mask: true,
				});
			},
			//延迟跳转到我的页面
			delayNavigate(){
				this.seconds = 3;
				this.showTips(this.seconds);
				
				this.timer = setInterval(() => {
					this.seconds--;
					
					if(this.seconds <= 0){
						//倒计时秒数小于等于0
						clearInterval(this.timer);
						
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart',
								});
							},
						});
						
						return;
					}
					
					this.showTips(this.seconds);
				}, 1000);
			},
			//支付
			async payOrder(){
				const orderInfo = {
					// orderprice: this.checkedTotalPrice,
					orderprice: 0.01,
					consigneeaddr: this.addrDetail,
					goods: this.cart.filter(x => x.goods_state).map(x => ({
						goodsid: x.goods_id,
						goodsnumber: x.goods_count,
						goodsprice: x.goods_price,
					})),
				}
				
				const {data: res} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo);
				
				if(res.meta.status === 200){
					//请求失败
					return uni.$showMsg('创建订单失败！');
				}
				
				res.message = {
					ordernumber: 'GD20180504000000000045',
				}
				
				const preOrderParam = {
					order_number: res.message.ordernumber,
				}
				
				const {data: resPreOrder} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', preOrderParam);
				
				if(resPreOrder.meta.status === 200){
					//请求失败
					return uni.$showMsg('订单预支付失败！');
				}
				
				resPreOrder.message = {
					pay: {
						"timeStamp": "1525681145",
						"nonceStr": "BkPggorBXZwPGXe3",
						"package": "prepay_id=wx071619042918087bb4c1d3d72999385683",
						"signType": "MD5",
						"paySign": "D1642DEEF1663C8012EDEB9297E1D516"
					},
				}
				
				const payInfo = resPreOrder.message.pay;
				const [err, succ] = await uni.requestPayment(payInfo);
				
				if(!err){
					//支付时发生错误
					return uni.$showMsg('订单未支付！');
				}
				
				const {data: resCheckOrder} = await uni.$http.post('/api/public/v1/my/orders/chkOrder', preOrderParam);
				
				if(resCheckOrder.meta.status === 200){
					//请求失败
					console.log('resCheckOrder:');
					console.log(resCheckOrder);
					return uni.$showMsg('订单未支付！');
				}
				
				uni.showToast({
					title: '订单支付完成！',
					icon: 'success',
				});
			},
		},
	}
</script>

<style lang="scss">
.my-settle-container {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	height: 50px;
	background-color: white;
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-left: 5px;
	font-size: 14px;
	.radio {
		display: flex;
		align-items: center;
	}
	.amount {
		text {
			color: #C00000;
			font-weight: bold;
		}
	}
	.btn-settle {
		background-color: #C00000;
		height: 50px;
		line-height: 50px;
		min-width: 100px;
		padding: 0 5px;
		color: white;
		text-align: center;
	}
}
</style>
